﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CurveMenuPanel
{
    public class Container : Control
    {

        TranslateTransform translateTransform;
        RotateTransform rotateTransform;

        Canvas LayoutRoot;

        public Container()
            : base()
        {

            DefaultStyleKey = typeof(Container);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();


            LayoutRoot = this.GetTemplateChild<Canvas>("LayoutRoot");

            var transformGroup = new TransformGroup();


            var transform = this.RenderTransform as TransformGroup;
            if (transform == null)
            {
                transformGroup = new TransformGroup();
                this.RenderTransform = transformGroup;

                translateTransform = new TranslateTransform();
                transformGroup.Children.Add(translateTransform);
            }



            //矫正
            var Correction = new TranslateTransform();
            Correction.X = -this.ActualWidth / 2;
            Correction.Y = -this.ActualHeight;
            transformGroup.Children.Add(Correction);




            var rtransform = this.RenderTransform as RotateTransform;
            if (rtransform == null)
            {
                rotateTransform = new RotateTransform();
                this.LayoutRoot.RenderTransform = rotateTransform;
            }
            this.LayoutRoot.RenderTransformOrigin = new Point(0.5, 0.5);
        }

        private T GetTemplateChild<T>(string childName) where T : DependencyObject
        {
            T childControl = this.GetTemplateChild(childName) as T;
            if (childControl == null)
            {
                throw new Exception(string.Format("Couldn't find {0}", childName));
            }
            return childControl;
        }



        public void addChildAt(FrameworkElement element, int zIndex)
        {
            this.LayoutRoot.Children.Add(element);
            element.SetValue(Canvas.ZIndexProperty, zIndex);
        }

        public void setChildIndex(FrameworkElement element, int zIndex)
        {
            element.SetValue(Canvas.ZIndexProperty, zIndex);
        }

        #region x (DependencyProperty)

        /// <summary>
        /// x
        /// </summary>
        public double x
        {
            get { return (double)GetValue(xProperty); }
            set { SetValue(xProperty, value); }
        }
        public static readonly DependencyProperty xProperty =
            DependencyProperty.Register("x", typeof(double), typeof(Container),
            new PropertyMetadata(0.0, new PropertyChangedCallback(OnxChanged)));

        private static void OnxChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((Container)d).OnxChanged(e);
        }

        protected virtual void OnxChanged(DependencyPropertyChangedEventArgs e)
        {
            translateTransform.X = (double)e.NewValue;
        }

        #endregion

        #region y (DependencyProperty)

        /// <summary>
        /// y
        /// </summary>
        public double y
        {
            get { return (double)GetValue(yProperty); }
            set { SetValue(yProperty, value); }
        }
        public static readonly DependencyProperty yProperty =
            DependencyProperty.Register("y", typeof(double), typeof(Container),
            new PropertyMetadata(0.0, new PropertyChangedCallback(OnyChanged)));

        private static void OnyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((Container)d).OnyChanged(e);
        }

        protected virtual void OnyChanged(DependencyPropertyChangedEventArgs e)
        {
            translateTransform.Y = (double)e.NewValue;
        }

        #endregion


        #region rotation (DependencyProperty)

        /// <summary>
        /// rotation
        /// </summary>
        public double rotation
        {
            get { return (double)GetValue(rotationProperty); }
            set { SetValue(rotationProperty, value); }
        }
        public static readonly DependencyProperty rotationProperty =
            DependencyProperty.Register("rotation", typeof(double), typeof(Container),
            new PropertyMetadata(0.0, new PropertyChangedCallback(OnrotationChanged)));

        private static void OnrotationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((Container)d).OnrotationChanged(e);
        }

        protected virtual void OnrotationChanged(DependencyPropertyChangedEventArgs e)
        {
            rotateTransform.Angle = (double)e.NewValue;
        }

        #endregion
    }
}
